package action;

import beans.UserFacadeLocal;
import com.opensymphony.xwork2.ActionSupport;
import entities.User;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import services.PageDetails;

/**
 *
 * @author corneliugosa
 */
public class ChangePassword extends ActionSupport implements ServletRequestAware{
    UserFacadeLocal userFacade = lookupUserFacadeLocal();
    
    private HttpServletRequest request;
    private User user;
    private String pass;
    private String confPass;
    private String message;
    private PageDetails pageDetails;
    
    public String execute(){
        HttpSession session = request.getSession();
        pageDetails = (PageDetails)session.getAttribute("pageDetails");
        
        if(pass.equals(confPass)){
            user = (User)session.getAttribute("user");
            user.setPassword(pass);
            
            userFacade.edit(user);
            pageDetails.setButtonPageDisplay("home");
            
            session.setAttribute("passwordChanged", "yes");
            session.setAttribute("user", user);           
            session.setAttribute("pageDetails", pageDetails);
            
            message = "The password has been successfully changed!";
            return SUCCESS;
        }

        message = "The entered passwords don't match!";
        return "failure";
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getConfPass() {
        return confPass;
    }

    public void setConfPass(String confPass) {
        this.confPass = confPass;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public PageDetails getPageDetails() {
        return pageDetails;
    }

    public void setPageDetails(PageDetails pageDetails) {
        this.pageDetails = pageDetails;
    }

    private UserFacadeLocal lookupUserFacadeLocal() {
        try {
            Context c = new InitialContext();
            return (UserFacadeLocal) c.lookup("java:global/School/School-ejb/UserFacade!beans.UserFacadeLocal");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
